package com.team.dutchboy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.team.common.dubbo.intf.DubboDemo;
import com.team.dutchboy.service.DutchboyService;
import com.team.dutchboy.service.FeignDemo;
import com.team.dutchboy.service.OauthCodeService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class DutchboyController {

    @Autowired
    private FeignDemo feignDemo;
    @Value("${var}")
    private String var;
    @Autowired
    private DutchboyService dutchboyService;

    @RequestMapping("feign")
    public String feign(){
        return feignDemo.callPush("test");
    }

    @RequestMapping("/t")
    public String t(){
        return var;
    }

    @RequestMapping("/t1")
    public String t(String a){
        return feignDemo.remoteTest(a);
    }
    //热点规则 流控demo
    @RequestMapping("hotRuleDemo/{param1}/{param2}")
    @SentinelResource("demo")
    public String hotRuleDemo(@PathVariable("param1") String param1,@PathVariable("param2")String param2){
        return param1+param2;
    }

    @RequestMapping("intf")
    public String intf(){
        int a= 5;
        return dutchboyService.test(a);
    }

    @Autowired
    private OauthCodeService oauthCodeService;

    @RequestMapping("seata")
    public String seata(){
        oauthCodeService.insert();
        return "测试seata分布式事务";
    }

    @Reference(group = "jk",timeout = 5000,retries = 1) //默认时间比较短可能会导致多次发送以及超时
    private DubboDemo dubboDemoConsumer;
    //dubbo事务测试
    @RequestMapping("dubbo")
    @GlobalTransactional
    public String dubbo(){
        oauthCodeService.insert();
        String s = dubboDemoConsumer.testDubbo();
        return "测试dubbo"+s;
    }

    //dubbo流控测试
    @RequestMapping("dubbos")
    public String dubbos(){
        String s = dubboDemoConsumer.testDubbo();
        return "测试dubbo";
    }




}
